package it.antonio.ilpizze.db.core.fs;

import it.antonio.ilpizze.db.core.perst.StrPerst;
import it.antonio.ilpizze.db.core.perst.UserPerst;
import it.antonio.ilpizze.db.index.hash.HashTable;
import it.antonio.ilpizze.db.index.hash.file.FileHashTable;
import it.antonio.ilpizze.db.io.IOFile;
import it.antonio.ilpizze.db.security.Role;
import it.antonio.ilpizze.db.security.User;
import it.antonio.ilpizze.func.Pair;

import java.util.Iterator;

public class SecurityFile {
	private IOFile file;
	private HashTable<String, User> users;
	
	
	
	public SecurityFile(IOFile file) {
		this.file = file;
	}

	public void init(){
		boolean firstOpen = file.size() == 0;
		users = FileHashTable.getOrCreate(file, 100, new StrPerst(), new UserPerst());
			
		if(firstOpen){
			users.put("ADMIN", new User("ADMIN", "ADMIN", Role.ADMIN));
		}	
	}

	public void put(String key, User value) {
		users.put(key, value);
	}

	public User get(String key) {
		return users.get(key);
	}

	public void remove(String key) {
		users.remove(key);
	}

	public Iterator<Pair<String, User>> iterator() {
		return users.iterator();
	}
	
	
}
